package com.ljh.onlinetest.controller.admin;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ljh.onlinetest.base.BaseApiController;
import com.ljh.onlinetest.base.RestResponse;
import com.ljh.onlinetest.domain.ExamPaper;
import com.ljh.onlinetest.domain.Subject;
import com.ljh.onlinetest.service.ExamPaperService;
import com.ljh.onlinetest.utils.DateTimeUtil;
import com.ljh.onlinetest.utils.PageInfoHelper;
import com.ljh.onlinetest.viewmodel.admin.paper.PaperEditRequestVM;
import com.ljh.onlinetest.viewmodel.admin.paper.PaperPageRequestVM;
import com.ljh.onlinetest.viewmodel.admin.paper.PaperResponseVM;
import com.ljh.onlinetest.viewmodel.admin.subject.SubjectResponseVM;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.function.Function;

/**
 * @author ljh
 * @ClassName ExamPaperController.java
 * @createTime 2022年01月18日
 */
@RestController("AdminExamPaperController")
@RequestMapping("/api/admin/exam/paper")
public class ExamPaperController extends BaseApiController {

    public static final Function<ExamPaper, PaperResponseVM> PAPER_RESPONSE_VM_FUNCTION = examPaper -> {
        PaperResponseVM paperResponseVm = modelMapper.map(examPaper, PaperResponseVM.class);
        paperResponseVm.setCreateTime(DateTimeUtil.dateFormat(examPaper.getCreateTime()));
        return paperResponseVm;
    };

    @Autowired
    private ExamPaperService examPaperService;

    @PostMapping("page")
    public RestResponse<PageInfo<PaperResponseVM>> page(@RequestBody PaperPageRequestVM pageRequestVM){
        PageInfo<ExamPaper> examPaperPageInfo = examPaperService.listPage(pageRequestVM);
        PageInfo<PaperResponseVM> paperResponseVmPageInfo = PageInfoHelper.copyMap(examPaperPageInfo, PAPER_RESPONSE_VM_FUNCTION);
        return RestResponse.ok(paperResponseVmPageInfo);
    }

    @PostMapping("/select/{id}")
    public RestResponse<PaperEditRequestVM> selectById(@PathVariable Integer id){
        PaperEditRequestVM paperEditRequestVM = examPaperService.examPaperToVM(id);
        return RestResponse.ok(paperEditRequestVM);
    }

    @PostMapping("/edit")
    public RestResponse<PaperEditRequestVM> edit(@RequestBody @Valid PaperEditRequestVM model){
        ExamPaper examPaper = examPaperService.savePaperFromVM(model,getCurrentUser());
        PaperEditRequestVM paperEditRequestVM = examPaperService.examPaperToVM(examPaper);
        return RestResponse.ok(paperEditRequestVM);
    }

    @PostMapping("/delete/{id}")
    public RestResponse delete(@PathVariable Integer id){
        ExamPaper examPaper = examPaperService.selectById(id);
        examPaper.setDeleted(true);
        examPaperService.updateByIdFilter(examPaper);
        return RestResponse.ok();
    }

    @PostMapping("/taskExamPage")
    public RestResponse<PageInfo<PaperResponseVM>> taskExamPageList(@RequestBody PaperPageRequestVM pageRequestVM){
        PageInfo<ExamPaper> pageInfo = examPaperService.taskExamPage(pageRequestVM);
        PageInfo<PaperResponseVM> vmPageInfo = PageInfoHelper.copyMap(pageInfo, e -> {
            PaperResponseVM paperResponseVM = modelMapper.map(e, PaperResponseVM.class);
            paperResponseVM.setCreateTime(DateTimeUtil.dateFormat(e.getCreateTime()));
            return paperResponseVM;
        });
        return RestResponse.ok(vmPageInfo);
    }
}
